package com.google.code.p.narcissus.coreIntegrationTests;

import java.io.File;
import java.io.IOException;

import junit.framework.Assert;

import org.apache.commons.io.FileUtils;
import org.apache.log4j.Logger;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.code.p.narcissus.core.BrowserConfiguration;
import com.google.code.p.narcissus.core.BrowserConfiguration.WindowSize;
import com.google.code.p.narcissus.core.NarcissusException;
import com.google.code.p.narcissus.core.ScreenShooter;
import com.google.code.p.narcissus.core.model.ComparisonResult.PictureComparisonResult;
import com.google.code.p.narcissus.coreIntegrationTests.utils.IntegrationTestConfiguration;
import com.thoughtworks.selenium.SeleneseTestCase;

public class SimpleScreenshotsIT extends SeleneseTestCase {

	private static final String USER_HOME = System.getProperty("user.home");

	private static final String NARCISSUS_INTEGRATION_TESTS_ROOT = "narcissusIntegrationTests";

	private static final String REFERENCES = "references";

	private static final String TEST_NAME = "simpleScreenshotTests";

	private static final String TEST_CLASS_NAME = SimpleScreenshotsIT.class.getSimpleName();

	private static final String SHOTS_ROOT = USER_HOME + "/" + NARCISSUS_INTEGRATION_TESTS_ROOT;

	private static final Logger LOGGER = Logger.getLogger(SimpleScreenshotsIT.class);

	@Before
	public void setUp() throws Exception {
		IntegrationTestConfiguration.getInstance().startServer();
		setUp("http://127.0.0.1:9876", "*firefox");
		copyReferencesToTestRoot();
	}

	private void copyReferencesToTestRoot() throws IOException {
		copyReferenceToTestRoot(new File(SimpleScreenshotsIT.class.getResource("/references/title.png").getFile()));
		copyReferenceToTestRoot(new File(SimpleScreenshotsIT.class.getResource("/references/subtitle.png").getFile()));
		copyReferenceToTestRoot(new File(SimpleScreenshotsIT.class.getResource("/references/leftMenuContainer.png").getFile()));
	}

	private void copyReferenceToTestRoot(File fileToCopy) throws IOException {
		File targetFolder = new File(USER_HOME + "/" + NARCISSUS_INTEGRATION_TESTS_ROOT + "/" + REFERENCES + "/" + TEST_CLASS_NAME + "/" + TEST_NAME);
		LOGGER.info("setUp: " + fileToCopy.getName() + " will be copied to " + targetFolder.getAbsolutePath());
		FileUtils.copyFileToDirectory(fileToCopy, targetFolder);
	}

	@After
	public void tearDown() throws Exception {
		IntegrationTestConfiguration.getInstance().stopServer();
		// FileUtils.deleteQuietly(new File(SHOTS_ROOT));
	}

	@Test
	public void testSimpleScreenshots() throws NarcissusException {
		// Test specific setup
		ScreenShooter screenShooter = new ScreenShooter(SHOTS_ROOT, TEST_CLASS_NAME, TEST_NAME, selenium);

		// Not strictly necessary. Each Selenium call is separated from the next one by that amount (this is a passthrough to a Selenium property).
		// Can be interesting when wanting to have the tests executed in an understandable manner (i.e. when you are checking the screen as the tests are executed)
		// ScreenShooter.setSeleniumSleepInterval(10);
		selenium.open("/");

		// Makes sure that the window will always have the same size when running the tests. A must when the design evolves with page size; Not that important if static.
		BrowserConfiguration.setWindowSize(selenium, WindowSize.Size_1280x1024);

		/*
		 * Actual test happens here:
		 * The first parameter is a Selenium locator pointing to the element you want to take a screenshot of and the second is the name the screenshot will be saved under.
		 * 
		 * You specify an element by Selenium locator => not tied to ids only (id is just the default).
		 * More info here (note that the version of the selenium core are old ones):
		 * - http://release.seleniumhq.org/selenium-remote-control/0.9.2/doc/dotnet/Selenium.html
		 * - http://release.seleniumhq.org/selenium-core/0.8.0/reference.html
		 * 
		 * The screenshot will be saved under the path you initialized when creating the new screenshooter:
		 * [SHOTS_ROOT]/[TEST_CLASS_NAME]/[TEST_NAME]/[SCREENSHOT_NAME]
		 */

		// A simple element with nothing special
		Assert.assertEquals(PictureComparisonResult.verificationSuccess, screenShooter.verifyGuiElement("title", "title"));

		// Another one, positioned by default
		Assert.assertEquals(PictureComparisonResult.verificationSuccess, screenShooter.verifyGuiElement("subtitle", "subtitle"));

		// A DIV element absolutely positioned under another one floating on the left
		Assert.assertEquals(PictureComparisonResult.verificationSuccess, screenShooter.verifyGuiElement("menu1", "leftMenuContainer"));
	}

}